package com.ha8.netease.views.team.body
{
	import com.ha8.netease.App;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.PublicNotify;
	import com.ha8.netease.models.vo.ViewModel;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.Event;
	
	/**
	 * body视图代理
	 * 
	 * @author rogo
	 */	
	public class BodyViewMediator extends Mediator
	{
		public function BodyViewMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'bodyViewMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var states:Array = [];
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function listNotificationInterests():Array
		{
			return [PublicNotify.DISPLAY_HOME_PAGE,
				PublicNotify.DISPLAY_MAIN_PAGE,
				PublicNotify.DISPLAY_USER_PAGE,
				PublicNotify.DISPLAY_PROFILE_PAGE,
				PublicNotify.DISPLAY_MAIL_PAGE,
				PublicNotify.DISPLAY_COMMENT_PAGE,
			 	PrivateNotify.DISPLAY_PRE_PAGE];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			if (notification.name == PrivateNotify.DISPLAY_PRE_PAGE)
			{
				prePage();
				return;
			}
			
			// 记录当前页
			recordState(view.currentState);
			
			switch (notification.name)
			{
				case PublicNotify.DISPLAY_HOME_PAGE:
				{
					view.currentState = 'homeView';
					break;
				}
				case PublicNotify.DISPLAY_MAIN_PAGE:
				{
					var bloglistModel:ViewModel = notification.param as ViewModel;
					if (bloglistModel)
					{
						App.getInstance().blogListModel = bloglistModel;
						view.currentState = 'mainView';
						if (view.mainPage)
							view.mainPage.dispatchEvent(new Event(Event.ADDED_TO_STAGE));
					}
					break;
				}
				case PublicNotify.DISPLAY_USER_PAGE:
				{
					var userlistModel:ViewModel = notification.param as ViewModel;
					if (userlistModel)
					{
						App.getInstance().userListModel = userlistModel;
						view.currentState = 'userView';
						if (view.userPage)
							view.userPage.dispatchEvent(new Event(Event.ADDED_TO_STAGE));
					}
					break;
				}
				case PublicNotify.DISPLAY_PROFILE_PAGE:
				{
					var profileModel:ViewModel = notification.param as ViewModel;
					if (profileModel)
					{
						App.getInstance().profileModel = profileModel;
						view.currentState = 'profileView';
						if (view.profileView)
							view.profileView.dispatchEvent(new Event(Event.ADDED_TO_STAGE));
					}
					break;
				}
				case PublicNotify.DISPLAY_MAIL_PAGE:
				{
					view.currentState = 'mailView';
					break;
				}
				case PublicNotify.DISPLAY_COMMENT_PAGE:
				{
					view.currentState = 'commentView';
					break;
				}
			}
			
			// 清楚重复页
			deleteState(view.currentState);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():BodyView
		{
			return viewComponent as BodyView;
		}
		
		private function recordState(state:String):void
		{
			deleteState(state);
			states.push(state);
		}
		
		private function deleteState(state:String):void
		{
			for (var index:int = 0; index < states.length; index++)
			{
				if (states[index] == state)
					states.splice(index, 1);
			}
		}
		
		private function prePage():void
		{
			if (states.length == 0)
				return;
			
			var preState:String = states.pop();
			view.currentState = preState;
		}
		
	}
}